package cn.cloud.mqtt2redis.controller;

import cn.cloud.mqtt2redis.configuration.mysql.progMapEntity;
import cn.cloud.mqtt2redis.service.MysqlService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class mysqlController {
    private Logger logger = LoggerFactory.getLogger(mysqlController.class);
    @Autowired
    private MysqlService mysqlService;


    //根据device和name找到对应的记录，判断文件是否被修改
    public boolean refresh_from_mysql(String device,String name,String prog_id,String time){
        try {
            if("".equals(time)) return false;
            progMapEntity record = mysqlService.query_by_deviceAndName(device,name);                        //查找记录
            if(record == null) {
                mysqlService.save_to_mysql(device,name,prog_id,time,"/IDEA/GFile/"+device+"/");//新建记录，直接插入
            }else {
                if(time.equals(record.getTime())){                                                           //修改时间未变，不做处理
                    return false;
                }
                else {
                    mysqlService.update_to_mysql(device,name,prog_id,time,"/IDEA/GFile/"+device+"/");                                                  //修改时间变化
                }
            }
        }catch (Exception e){
            logger.error("request of query by device and name failed!"+e);
            return false;
        }
        return true;
    }
}
